﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using TomShane.Neoforce.Controls;

namespace ClickGame.GUI.Controls
{
    public class AdvancedStackPanel : Panel
    {
        #region //// Fields ////////////

        ////////////////////////////////////////////////////////////////////////////
        private Orientation orientation;
        private bool _overflowVertical;
        private bool _overflowHorizontal;
        private Label _textLabel;
        ////////////////////////////////////////////////////////////////////////////

        #endregion

        #region //// Constructors //////

        ////////////////////////////////////////////////////////////////////////////
        public AdvancedStackPanel(Manager manager, Orientation orientation)
            : base(manager)
        {
            this.orientation = orientation;
            _textLabel = new Label(manager);
            ClientArea.ParentChanged += ClientArea_ParentChanged;
            //this.Color = Color.Transparent;
        }

        void ClientArea_ParentChanged(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            CalcLayout();
        }
        ////////////////////////////////////////////////////////////////////////////

        #endregion

        public override void Init()
        {
            _textLabel.Init();
            _textLabel.Top = 2;
            _textLabel.Left = 2;
            _textLabel.AutoSize = true;
            Add(_textLabel);

            base.Init();
        }

        public override string Text 
        {
            get { return base.Text; }
            set
            {
                base.Text = value;
                _textLabel.Text = value;
            }
        }

        public bool OverflowVertical
        {
            get { return _overflowVertical; }
            set
            {
                _overflowVertical = value;
                CalcLayout();
            }
        }

        public bool OverflowHorizontal
        {
            get { return _overflowHorizontal; }
            set
            {
                _overflowHorizontal = value;
                CalcLayout();
            }
        }

        #region //// Methods ///////////

        public override void Add(Control control)
        {
            base.Add(control);
            CalcLayout();

        }

        public override void Add(Control control, bool client)
        {
            base.Add(control, client);
            CalcLayout();

        }

        ////////////////////////////////////////////////////////////////////////////
        private void CalcLayout()
        {
            if (_textLabel == null)//not initialized yet
            {
                return;
            }
            int top = _textLabel.AbsoluteRect.Height + 2 + _textLabel.Top;
            int left = 0;

            foreach (Control c in ClientArea.Controls)
            {
                if(c == _textLabel)
                    continue;
                Margins m = c.Margins;

                if (orientation == Orientation.Vertical)
                {

                    if (!OverflowVertical)
                    {
                        if (c.Top + c.Height + m.Bottom + m.Top > Height || top + c.Height + m.Bottom + m.Top > Height)
                        {
                            top = _textLabel.AbsoluteRect.Height + 2 + _textLabel.Top;
                            left += m.Left;
                            left += c.Width;
                            left += m.Right;
                        }
                    }
                    top += m.Top;
                    c.Top = top;
                    top += c.Height;
                    top += m.Bottom;
                    c.Left = left;
                }

                if (orientation == Orientation.Horizontal)
                {
                    if (!OverflowHorizontal)
                    {
                        if (c.Left + c.Width + m.Right + m.Left > Width || left + c.Width + m.Right + m.Left > Width)
                        {
                            left = 0;
                            top += m.Top;
                            top += c.Height;
                            top += m.Bottom;
                        }
                    }
                    left += m.Left;
                    c.Left = left;
                    left += c.Width;
                    left += m.Right;
                    c.Top = top;
                }
            }
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            base.DrawControl(renderer, rect, gameTime);
        }
        ////////////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////////////
        protected override void OnResize(ResizeEventArgs e)
        {
            CalcLayout();
            base.OnResize(e);
            
        }
        ////////////////////////////////////////////////////////////////////////////

        #endregion
    }
}
